package com.myk.game.heroscuffle.skills;

import com.myk.game.heroscuffle.game.HateRecorder;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Skill;

public class Skill_ZhanJue extends Skill {

    public final int BASE_DAMAGE = 100; //基础伤害值
    public final int ADD_DAMAGE_UNIT = 10; //目标每损失100点hp的伤害增加量
    private Hero intentTarget;

    public Skill_ZhanJue() {
        super("斩决", "对目标造成大量伤害，且目标生命值越低，造成的伤害越高。", 3, Integer.MAX_VALUE);
    }

    public void setIntentTarget(Hero intentTarget) {
        this.intentTarget = intentTarget;
    }

    @Override
    protected void onTrigger() {
        if (hostHero.state.isPlayer) {
            selectTargetHero(0, true);
        }
        else {
            if (intentTarget == null) {
                intentTarget = game.pool.getAnyoneAliveHero(hostHero);
            }
            skillEffect(intentTarget);
        }
    }

    //玩家选择技能施放目标
    private void selectTargetHero(final int index, final boolean isFirst) {
        game.executor.submit(() -> {
            Hero curSelectHero = game.pool.getAllAliveHeroes().get(index);
            if (curSelectHero == hostHero) {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }
            game.ui.setTip("请选择你要斩击的目标,当前待选:" + curSelectHero.getObviousName());
            game.ui.resetCtrl();
            game.ui.addCtrl("选中", () -> game.executor.submit(() -> {
                skillEffect(curSelectHero);
                game.executor.resume(); //通知阻塞的线程继续执行以后的代码
            }));
            game.ui.addCtrl("切换", () -> game.executor.submit(() -> {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }));
        });
        if (isFirst) { //防止因为多次切换而阻塞多个线程
            game.executor.pause(); //阻塞线程直到接收到通知
        }
    }

    //技能效果
    private void skillEffect(Hero target) {
        int multiple = (target.HP - target.hp) / 100;
        int damage = BASE_DAMAGE + multiple * ADD_DAMAGE_UNIT;
        game.ui.printMsg(target.getObviousName() + "还没反应过来,身上就多了一道深深的伤口,");
        target.sufferDamage(null, this, null, damage);
        if (target.state.isAlive) {
            //target.setEnemy(hostHero);
            game.recorder.record(game.round,hostHero,target, HateRecorder.HATE_TYPE_EFFECT);
        }
    }

    @Override
    protected void onUpdate() {

    }

    @Override
    protected void onInit() {

    }
}
